import * as echarts from '../../../../common/page/ec-canvas/echarts';
const app = getApp();
import {
  Toast
} from 'wuss-weapp';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ec: {
      lazyLoad: true
    },
    isShow: false,
    obj: {
      content: ""
    },
    resultComList: [],
    touchMove: 0,
    touchStart: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id,
      theme: app.globalData.theme
    });
    wx.setNavigationBarTitle({
      title: '讨论议事',
    })
    wx.getSystemInfo({
      success: res => {
        this.setData({
          height: res.windowHeight - 50
        });
      }
    });
    this.getDetail();
    this.getCommentList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    app.globalData.pageNumber = 1;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    app.globalData.pageNumber++;
    this.setData({
      mpLoadingShow: true,
      mpLoadingTips: "加载中..."
    });
    if (app.globalData.pageNumber <= app.globalData.pageTotalNumber) {
      this.getCommentList();
    } else {
      setTimeout(() => {
        this.setData({
          mpLoadingTips: "暂无数据"
        });
      }, 500);
      setTimeout(() => {
        this.setData({
          mpLoadingShow: false
        });
      }, 1000);
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getDetail: function () {
    app.wxGetRequest("/wechart/life/getMicroservice", {
      id: this.data.id,
      url: app.globalData.urlMicro + "/v1/conference"
    }).then(result => {
      this.setData({
        result: result.data[0]
      });
      // 获取组件
      this.ecComponent = this.selectComponent('#mychart-dom-bar');
      this.initChart(result.data[0]);
    });
  },
  /**
   * 提交投票
   */
  saveConferenceDetails: function (e) {
    let type = e.currentTarget.dataset.type;
    let title = e.currentTarget.dataset.title;
    app.confirm("温馨提示", "您确认提交『" + title + "』投票吗？").then(result => {
      app.wxPostRequest("/wechart/life/postMicroservice", {
        url: app.globalData.urlMicro + "/v1/conference_detail",
        conference_id: this.data.id,
        user_id: app.globalData.userInfo["user_id"],
        vote: type,
        creator_id: app.globalData.userInfo["user_id"],
        creator_name: "超级管理员"
      }).then(result => {
        console.error(result)
        if (result.data.is_ok){
          this.getDetail();
          Toast.show({
            type: 'success',
            message: result.data.message,
          });
        }else{
          Toast.show({
            type: 'success',
            message: result.data.error,
          });
        }
       
      });
    });
  },
  initChart: function (data) {
    this.ecComponent.init((canvas, width, height) => {
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height
      });
      let option = {
        title: {
          subtext: '票数'
        },
        tooltip: {
          trigger: 'axis'
        },
        grid: {
          top: "7%",
          left: '3%',
          right: '3%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          data: ['赞成', '拒绝', '弃票']
        },
        yAxis: [
          {
            type: 'value',
            splitLine: {
              show: false
            },
            splitArea: {
              show: true,
            },
          }
        ],
        series: [{
          type: 'bar',
          barWidth: '40%',
          itemStyle: {
            normal: {
              color: function (params) {
                let colorList = ['#1AAD19', '#E64340', '#1F78B4'];
                return colorList[params.dataIndex]
              },
              label: {
                show: true,
                position: 'top',
                formatter: '{b}({c})'
              }
            }
          },
          data: [data.assentient, data.rejection, data.abdicator]
        }]
      };
      chart.setOption(option);
      // 注意这里一定要返回 chart 实例，否则会影响事件处理等
      return chart;
    });
  },
  onContent: function (e) {
    let value = e.currentTarget.dataset.value;
    if (e.detail.value.replace(/\s+/g, '').length == 0) {
      this.setData({
        ["obj." + value + ""]: ""
      });
      return "";
    } else {
      this.setData({
        ["obj." + value + ""]: e.detail.value.replace(/\s+/g, '')
      });
    }
  },
  getCommentList: function () {
    app.wxGetRequest("/wechart/life/getMicroservice", {
      url: app.globalData.urlMicro + "/v1/conference_critic",
      conference_id: this.data.id,
      page_number: app.globalData.pageNumber,
      page_size: app.globalData.pageSize
    }).then(result => {
      let pageTotalNumber = Math.ceil(result.data.count / app.globalData.pageSize)
      this.setData({
        resultComList: this.data.resultComList.concat(result.data.records),
        noData: pageTotalNumber == 0 ? true : false,
        mpLoadingShow: false
      });
      app.globalData.pageTotalNumber = pageTotalNumber;
    });
  },
  //textarea聚焦
  onBindFocus: function () {
    this.setData({
      isShow: true
    });
  },
  //textarea失焦
  onBindBlur: function () {
    this.setData({
      isShow: false
    });
  },
  //点击回复
  onTapReply: function () {
    this.setData({
      isShow: true
    });
  },
  //回复
  onPublish: function () {
    app.wxPostRequest("/wechart/life/postMicroservice", {
      url: app.globalData.urlMicro + "/v1/conference_critic",
      conference_id: this.data.id,
      user_id: app.globalData.userInfo["user_id"],
      content: this.data.obj["content"],
      creator_id: app.globalData.userInfo["user_id"],
      creator_name: "小程序"
    }).then(result => {
      this.setData({
        isShow: false,
        ['obj.content']: "",
        resultComList: [result.data.data].concat(this.data.resultComList)
      });
      Toast.show({
        type: 'success',
        message: result.data.message,
      });
    });
  },
})